<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Videos extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('manage_login');
        $this->load->model('share_media');
        $this->load->model('manage_comments');
        $this->load->model('manage_follow');
        $this->load->model('get_profile_infos');
        
    }
    public function index() {
        $videoID = $this->uri->segment(2);
        if($this->share_media->getVideo($videoID) && $videoID != ""){
            $this->video($videoID);
        }
        elseif($this->uri->segment(2)){
            echo "Error 404";
        }else{
            $data = $this->manage_login->manageLogin();
            if (!$this->input->get('cat') || $this->input->get('cat') === 'popular') {
                $videos = $this->share_media->getPopVideos();
            } else if ($this->input->get('cat') === 'choice') {
                $videos = $this->share_media->getChoiceVideos();
            } elseif ($this->input->get('cat') === 'fresh') {
                $videos = $this->share_media->getFreshVideos();
            } elseif ($this->input->get('cat') === 'upcoming') {
                $videos = $this->share_media->getUpcomingVideos();
            } elseif ($this->input->get('cat') === 'favorite') {
                $videos = $this->share_media->getFavVideos();
            }
            $data['videos'] = $videos;
            $this->lang->load('main', 'english');
            foreach ($data['videos'] as $key => $value) {
                $data['videos'][$key]['video_infos'] = $this->xhrVideo($value['video_id']);
            }
            $this->load->view('video/video', $data);
        }
    }
    function video($videoID){
        $video = $this->share_media->getVideo($videoID);
        $user = $this->get_profile_infos->getInfosID($video[0]["user_id"]);
        $likes = $this->getLikes($videoID);
        $favs = $this->getFavs($videoID);
        $didLike = $this->share_media->didLikeVid($videoID,$this->input->cookie('LEAPSESSID'));
        $didFav = $this->share_media->didFavVid($videoID,$this->input->cookie('LEAPSESSID'));
        $comments = $this->manage_comments->getcomments('video',$videoID);
        $data = $this->manage_login->manageLogin();
        $data['isFollowing'] = $this->manage_follow->isFollowing($this->input->cookie('LEAPSESSID'),$video[0]["user_id"]);
        $data['video_infos'] = $video[0];
        $data['user'] = $user;
        $data['likes'] = $likes;
        $data['favs'] = $favs;
        $data['did_like'] = $didLike;
        $data['did_fav'] = $didFav;
        $data['comments'] = $comments;
        $data['connected'] = $this->input->cookie('LEAPSESSID');
        $data['facebook_og'] = '<meta property="fb:app_id" content="264489850252668" /> 
              <meta property="og:type" content="leap_pk:parkour_video" /> 
              <meta property="og:title" content="'.$data['video_infos']['title'].'" /> 
              <meta property="og:image" content="'.$data['video_infos']['thumb'].'" /> 
              <meta property="og:description" content="'.$data['video_infos']['description'].'" /> 
              <meta property="og:url" content="http://leapdemo.com/videos/'.$data['video_infos']['video_id'].'">';
        $this->load->view('video/videodetails', $data);
    }
    function xhrVideo($videoID){
        $video = $this->share_media->getVideo($videoID);
        $user = $this->get_profile_infos->getInfosID($video[0]["user_id"]);
        $likes = $this->getLikes($videoID);
        $favs = $this->getFavs($videoID);
        $didLike = $this->share_media->didLikeVid($videoID,$this->input->cookie('LEAPSESSID'));
        $didFav = $this->share_media->didFavVid($videoID,$this->input->cookie('LEAPSESSID'));
        $comments = $this->manage_comments->getcomments('video',$videoID);
        if($this->input->cookie('LEAPSESSID', TRUE))
            $data['isFollowing'] = $this->manage_follow->isFollowing($this->input->cookie('LEAPSESSID'),$video[0]["user_id"]);
        $data['video'] = $video[0];
        $data['user'] = $user;
        $data['likes'] = $likes;
        $data['favs'] = $favs;
        $data['did_like'] = $didLike;
        $data['did_fav'] = $didFav;
        $data['comments'] = $comments;
        $data['connected'] = $this->input->cookie('LEAPSESSID');
        return $data;
    }
    function getVideos(){
      $videos = $this->share_media->getVideos();
      echo json_encode($videos);
    }
    function getLikes($videoID){
      
      $likes = $this->share_media->getLikesVid($videoID);
      return $likes;
    }
    function getFavs($videoID){
      
      $favs = $this->share_media->getFavsVid($videoID);
      return $favs;
    }
    function like(){
        $video_id = $this->input->post("video_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->likeVideo($video_id,$user_id);
        echo $query;
    }
    function dislike(){
        $video_id = $this->input->post("video_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->dislikeVideo($video_id,$user_id);
        echo $query;
    }
    function favorite(){
        $video_id = $this->input->post("video_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->favoriteVideo($video_id,$user_id);
        echo $query;
    }
    function dlFavorite(){
        $video_id = $this->input->post("video_id");
        $user_id = $this->input->cookie('LEAPSESSID');
        $query = $this->share_media->rmvFavoriteVid($video_id,$user_id);
        echo $query;
    }
}
